﻿using Microsoft.AspNetCore.Mvc;
using seejee_Backendium.Core.Interfaces;
using seejee_Backendium.Core.Models;
using seejee_Backendium.Data.Services;

namespace seejee_Backendium.WEB.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BIPermController : Controller
    {
        private readonly ILogger<BIPermController> _logger;
        private readonly IBIPermissionsService _bipermService;
        public BIPermController(ILogger<BIPermController> logger, IBIPermissionsService bipermService)
        {
            _logger = logger;
            _bipermService = bipermService;
        }
        [HttpGet]
        public async Task<IActionResult> GetAllBIPerm()
        {
            try
            {
                _logger.LogInformation("开始获取所有客户的BI权限数据");
                var result = await _bipermService.GetAllAsync();

                if (!result.Any())
                {
                    _logger.LogWarning("未查询到任何客户的BI权限数据");
                    return NoContent(); // 无数据时返回204 No Content
                }

                _logger.LogInformation("成功获取所有客户的BI权限数据，共{Count}条", result.Count());
                return Ok(result); // 用Ok()包装结果，返回200 OK
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取客户BI权限数据时发生异常");
                return StatusCode(500, new { message = "获取权限数据失败", error = ex.Message });
            }
        }
        [HttpGet("view/viewName")]
        public async Task<IActionResult> GetBIPerm(string cCusName)
        {
            try
            {
                _logger.LogInformation("开始获取单个的BI权限数据");
                var result = await _bipermService.GetBIPerm(cCusName);

                if (!result.Any())
                {
                    _logger.LogWarning("未查询到任何客户的BI权限数据");
                    return NoContent(); // 无数据时返回204 No Content
                }

                _logger.LogInformation("成功获取所有客户的BI权限数据，共{Count}条", result.Count());
                return Ok(result); // 用Ok()包装结果，返回200 OK
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取客户BI权限数据时发生异常");
                return StatusCode(500, new { message = "获取权限数据失败", error = ex.Message });
            }
        }

        // 新增：获取一级菜单
        [HttpGet("primaryMenus")]
        public async Task<IActionResult> GetPrimaryMenus()
        {
            var menus = await _bipermService.GetPrimaryMenusAsync();
            return Ok(menus);
        }

        // 新增：获取二级菜单
        [HttpGet("secondaryMenus")]
        public async Task<IActionResult> GetSecondaryMenus()
        {
            var menus = await _bipermService.GetSecondaryMenusAsync();
            return Ok(menus);
        }

        // 新增：更新客户BI权限
        [HttpPut]
        public async Task<IActionResult> UpdateBIPerm([FromBody] BIQuanXianDto dto)
        {
            try
            {
                var result = await _bipermService.UpdateAsync(dto);
                return Ok(result);
            }
            catch (KeyNotFoundException ex)
            {
                return NotFound(ex.Message);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新权限失败");
                return StatusCode(500, "更新权限失败");
            }
        }

        // 新增：更新客户BI权限
        [HttpPut("DB")]
        public async Task<IActionResult> UpdateBIDBPerm([FromBody] BIDBpermissions dto)
        {
            try
            {
                var result = await _bipermService.UpdataBIPermissionsAsync(dto);
                return Ok(result);
            }
            catch (KeyNotFoundException ex)
            {
                return NotFound(ex.Message);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新权限失败");
                return StatusCode(500, "更新权限失败");
            }
        }
    }
}
